<!-- 修改密码 -->
<template>
	<view class="uni-content">

    <div>
			<!-- 顶部文字 -->
			<text v-if="hasPwd" class="title title-box">修改密码</text>
			<text v-if="!hasPwd" class="title title-box">设置新密码</text>
    </div>

		<uni-forms ref="form" :value="formData" err-show-type="toast">
			<uni-forms-item v-if="hasPwd" name="oldPassword">
				<uni-easyinput :focus="focusOldPassword" @blur="focusOldPassword = false" class="input-box"
					type="password" :inputBorder="false" v-model="formData.oldPassword" placeholder="请输入旧密码">
				</uni-easyinput>
			</uni-forms-item>

			<uni-forms-item name="newPassword">
				<uni-easyinput :focus="focusNewPassword" @blur="focusNewPassword = false" class="input-box"
					type="password" :inputBorder="false" v-model="formData.newPassword" placeholder="请输入新密码">
				</uni-easyinput>
			</uni-forms-item>

			<uni-forms-item name="newPassword2">
				<uni-easyinput :focus="focusNewPassword2" @blur="focusNewPassword2 = false" class="input-box"
					type="password" :inputBorder="false" v-model="formData.newPassword2" placeholder="请再次输入新密码">
				</uni-easyinput>
			</uni-forms-item>

			<button class="uni-btn send-btn-box" type="primary" @click="submit">提交</button>
		</uni-forms>
	</view>
</template>

<script>
var app = getApp();
	import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
  import passwordMod from '@/uni_modules/uni-id-pages/common/password.js'
  const uniIdCo = uniCloud.importObject("uni-id-co", {
		customUI:true
	})
	export default {
		mixins: [mixin],
		data() {
			return {
        hasPwd:false,

				focusOldPassword: false,
				focusNewPassword: false,
				focusNewPassword2: false,
				formData: {
					'oldPassword': '',
					'newPassword': '',
					'newPassword2': '',
				},
				rules: {
					// oldPassword: {
					// 	rules: [{
					// 			required: true,
					// 			errorMessage: '请输入新密码',
					// 		},
					// 		{
					// 			pattern: /^.{6,20}$/,
					// 			errorMessage: '密码为6 - 20位',
					// 		}
					// 	]
					// },
          // ...passwordMod.getPwdRules('newPassword', 'newPassword2')
        },
				logo: "/static/logo.png"
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		onShow() {
			// #ifdef H5
			document.onkeydown = event => {
				var e = event || window.event;
				if (e && e.keyCode == 13) { //回车键的键值为13
					this.submit()
				}
			};
			// #endif
		},
    onLoad(t) {
      if (t.hasPwd==1) {
        this.hasPwd = true
      }
    },
		methods: {
			/**
			 * 完成并提交
			 */
			submit() {
				this.$refs.form.validate()
					.then(res => {
						let {
							oldPassword,
							newPassword
						} = this.formData

            app.globalData.util.request({
              url: 'entry/wxapp/update_user_userinfo',
              cachetime: '0',
              data: {
                old_password: oldPassword,
                password: newPassword
              },
              success: (res)=> {
                uni.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
                if(res.data.code==0){
                  return
                }
                setTimeout(()=>{
                  uni.navigateBack({
                    delta: 1
                  })
                  // uni.reLaunch({
                  //   url: '/ymq_canyino2o/pages/my2/index'
                  // })
                },500)
              }
            })

					}).catch(errors => {
						let key = errors[0].key
						key = key.replace(key[0], key[0].toUpperCase())
						this['focus' + key] = true
					})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/uni_modules/uni-id-pages/common/login-page.scss";

	@media screen and (max-width: 690px) {
		.uni-content{
			margin-top: 15px;
		}
	}

	@media screen and (min-width: 690px) {
		.uni-content{
			padding: 30px 40px 40px;
		}
	}
</style>
